//
//  XJAuthViewController.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/20.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class XJAuthViewController: UIViewController {
    
    override func loadView() {
        let urlSting = "https://api.weibo.com/oauth2/authorize?client_id=\(APPKEY)&redirect_uri=\(APPREDIRECT_URI)"
        let url = NSURL(string: urlSting)
        guard let u = url else {
            return
        }
        
        let request = NSURLRequest(URL: u)
        
        webView.loadRequest(request)
        webView.delegate = self
        view = webView
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    func setupUI(){
        view.backgroundColor = UIColor.whiteColor()
        navigationController?.view.insertSubview(progressView, belowSubview: navigationController!.navigationBar)
        setupNav()
        
    }
    
    private func setupNav() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", imgName: nil, target: self, action: "cancelClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", imgName: nil, target: self, action: "autoFillClick")
        navigationItem.title = "微博登录"
    }
    
    @objc private func cancelClick(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func autoFillClick() {
        let jsString = "document.getElementById('userId').value='\(wbName)',document.getElementById('passwd').value='\(wbPassword)'"
        
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    //懒加载控件
    lazy var webView: UIWebView = UIWebView()
    lazy var progressView: XJOAuthProgressView = XJOAuthProgressView()
    
}

extension XJAuthViewController: UIWebViewDelegate{
    //开始加载
    func webViewDidStartLoad(webView: UIWebView) {
        
        //        SVProgressHUD.show()
        progressView.startAnim()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlStrig = request.URL?.absoluteString
        
        if let u = urlStrig where u.hasPrefix(APPREDIRECT_URI){
            //url的请求参数
            let query = request.URL?.query
            
            if let q = query {
                //获取code
                let code = q.substringFromIndex("code=".endIndex)
              
                XJUserAccountViewModel.sharedTools.loadUserAccount(code, isSuccess: { (isSucc) -> () in
                    if !isSucc{
                        return
                    }
                    //先关闭当前界面
                    self.dismissViewControllerAnimated(false, completion: { () -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: nil)
                    })
                })
                return false
            }
        }
        
        return true
    }
    //结束加载
    func webViewDidFinishLoad(webView: UIWebView) {
        
        // SVProgressHUD.dismiss()
        progressView.endAnim()
    }
}

extension XJAuthViewController {
    
//    func getUserAccount(code :String) {
//        
//        XJNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> () in
//
//            }) { (error) -> () in
//                
//        }
//    }
}











